using System;
using System.IO;
using System.Windows.Forms;

namespace FileMergerWinForms
{
    public partial class Form1 : Form
    {
        private string file1Path;
        private string file2Path;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            // 创建OpenFileDialog实例并设置其属性
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            OpenFileDialog openFileDialog2 = new OpenFileDialog();

            // 显示第一个文件选择对话框
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                file1Path = openFileDialog1.FileName;
            }

            // 显示第二个文件选择对话框
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                file2Path = openFileDialog2.FileName;
            }

            // 合并文件内容
            if (!string.IsNullOrEmpty(file1Path) && !string.IsNullOrEmpty(file2Path))
            {
                MergeFiles(file1Path, file2Path);
            }
            else
            {
                MessageBox.Show("请选择两个文件来合并。");
            }
        }

        private void MergeFiles(string path1, string path2)
        {
            // 确保Data目录存在
            string dataDirectory = Path.Combine(Application.StartupPath, "Data");
            if (!Directory.Exists(dataDirectory))
            {
                Directory.CreateDirectory(dataDirectory);
            }

            // 创建新文件名
            string outputFileName = $"MergedFile_{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
            string outputPath = Path.Combine(dataDirectory, outputFileName);

            // 读取文件内容并合并
            try
            {
                string file1Content = File.ReadAllText(path1);
                string file2Content = File.ReadAllText(path2);

                // 将内容写入到新文件中
                File.WriteAllText(outputPath, file1Content + Environment.NewLine + file2Content);

                MessageBox.Show($"文件已成功合并到 {outputPath}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"合并文件时出错: {ex.Message}");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 初始化按钮文本
            btnMerge.Text = "选择两个文件并合并";
        }
    }
}
